<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class contents_model extends CI_Model {
	//모델 생성자 호출
	function contents_model(){
		parent::__construct();
		$this->load->helper('file');
		$this->load->model(array('menu_model'));
	}
	
	/**
	* create contents file
	*
	* @access	public
	* @return	none
	*/
	function create(){
		$auth_write = null;
		$content = $this->input->post('content');
		$page_type = $this->input->post('page_type');
		$id = $this->input->post('id');
		/*$plugin = null;
		if($this->input->post('plugin')){
			$plugin = '{"plugin" : "'.$this->input->post('plugin').'"}' ;
		}*/
		$plugin =  $this->input->post('plugin');
		
		$data = array(
				'menu_id' => $id  ,
				'page_type' => $page_type ,
				'title' => $this->input->post('title') ,
				'keyword' => $this->input->post('keyword') ,
				'description' => $this->input->post('description') ,
				'content' => $content ,
				'plugin' => $plugin,
				'reg_date' => date("Y-m-d H:i",time()) ,
				'reg_uid' => 'admin'
		);
		
		$this->db->insert('ch_config_contents', $data);
		
		//메뉴 기능별 설정 등록
		switch($page_type){
			case('HTML'):  //일반 HTML 파일 생성
				write_file($this->config->item('ch_path_html').$id.'.html', $content);
			break;
			case('BOARD'): //게시판 설정 등록
				$cfg = array(
						'menu_id' => $id,
						'board_skin' => $this->input->post('board_skin'),
						'limit_title' => $this->input->post('limit_title'),
						'list_cnt' => $this->input->post('list_cnt'),
						'secret_yn' =>  $this->input->post('secret_yn'),
						'auth_write' => $this->input->post('auth_write'),
						'auth_view' => $this->input->post('auth_view'),
						'auth_reply' => $this->input->post('auth_reply'),
						'auth_comment' => $this->input->post('auth_comment'),
					);
					//$this->db->insert('ch_config_board', $cfg);
					$table_board = read_file($this->config->item('ch_sql_board'));
					$table_board = str_replace('[##_table_##]',$id,$table_board);
					$this->db->query($table_board);
					
					$table_board = read_file($this->config->item('ch_sql_board_comment'));
					$table_board = str_replace('[##_table_##]',$id,$table_board);
					$this->db->query($table_board);
					
			break;
			case('GALLERY'): //갤러리 설정 등록
				$cfg = array(
						'menu_id' => $id,
						'gallery_skin' => $this->input->post('gallery_skin'),
						'limit_title' => $this->input->post('limit_title'),
						'cnt_row' => $this->input->post('cnt_row'),
						'cnt_col' => $this->input->post('cnt_col'),
						'thumbnail_width' => $this->input->post('thumbnail_width'),
						'thumbnail_height' => $this->input->post('thumbnail_height'),						
						'secret_yn' =>  $this->input->post('secret_yn'),
						'auth_write' => $this->input->post('auth_write'),
						'auth_view' => $this->input->post('auth_view'),
						'auth_comment' => $this->input->post('auth_comment'),
				);
				$this->db->insert('ch_config_gallery', $cfg);
				$table_board = read_file($this->config->item('ch_sql_gallery'));
				$table_board = str_replace('[##_table_##]',$id,$table_board);
				$this->db->query($table_board);
			break;
		}
		
		//메뉴XML생성
		$this->menu_model->create();
	
	}
	
	function select($id){
  		$data['dbjob'] = 'i';
  		$data['title'] = null;
  		$data['page_type'] = null;
  		$data['content'] = null;
  		$data['keyword'] = null;
  		$data['description'] = null;
  		$data['board_skin'] = null;
  		$data['limit_title'] = null;
  		$data['list_cnt'] = null;
  		$data['secret_yn'] = null;
  		$data['auth_write'] = null;
  		$data['auth_view'] = null;
  		$data['auth_reply'] = null;
  		$data['auth_comment'] = null;
  		$data['cnt_row'] = null;
  		$data['cnt_col'] = null;
  		$data['thumbnail_width'] = null;
  		$data['thumbnail_height'] = null;
  		$data['content'] = null;
  		  		
	  	if($id){	
	  		$this->db->select("parent_id");
	  		$this->db->where('id', $id);
	  		$query = $this->db->get('tree');
	  		$row = $query->row_array();
	  		$data['parent_id'] = $row['parent_id'];
	  		$query->free_result();
	  		
	  		$this->db->select("*");
	  		$this->db->where('menu_id', $id);
	  		$query = $this->db->get('ch_config_contents');
	  	
	  		if($query->num_rows() > 0){
		  		$row = $query->row_array();
		  		$data['dbjob'] = 'u';
		  		$data['title'] = $row['title'];
		  		$data['page_type'] = $row['page_type'];
		  		$data['keyword'] = $row['keyword'];
		  		$data['description'] = $row['description'];
		  		$data['plugin'] = $row['plugin'];
		  		$page_type = $row['page_type'];
	  		
		  		switch($page_type){
		  			case('HTML'):
		  				$data['content'] = read_file(convert_path('/application/views/content/'.$id.'.html'));
		  			break;
		  			case('BOARD'):
		  				$this->db->select("*");
		  				$this->db->where('menu_id', $id);
		  				$query = $this->db->get('ch_config_board');
		  				$row1 = $query->row_array();
		  					
		  				$data['board_skin'] = $row1['board_skin'];
		  				$data['limit_title'] = $row1['limit_title'];
		  				$data['list_cnt'] = $row1['list_cnt'];
		  				$data['secret_yn'] = ($row1['secret_yn'] == 'Y') ? 'checked':'';
		  				$data['auth_write'] = $row1['auth_write'];
		  				$data['auth_view'] = $row1['auth_view'];
		  				$data['auth_reply'] = $row1['auth_reply'];
		  				$data['auth_comment'] = $row1['auth_comment'];
		  			break;
		  			case('GALLERY'):
		  				$this->db->select("*");
		  				$this->db->where('menu_id', $id);
		  				$query = $this->db->get('ch_config_gallery');
		  				$row1 = $query->row_array();
		  		
		  				$data['gallery_skin'] = $row1['gallery_skin'];
		  				$data['limit_title'] = $row1['limit_title'];
		  				$data['cnt_row'] = $row1['cnt_row'];
		  				$data['cnt_col'] = $row1['cnt_col'];
		  				$data['thumbnail_width'] = $row1['thumbnail_width'];
		  				$data['thumbnail_height'] = $row1['thumbnail_height'];
		  				$data['secret_yn'] = ($row1['secret_yn'] == 'Y') ? 'checked':'';
		  				$data['auth_write'] = $row1['auth_write'];
		  				$data['auth_view'] = $row1['auth_view'];
		  				$data['auth_comment'] = $row1['auth_comment'];
		  			break;
		  			
		  		}
		  		 
		  		switch($page_type){
		  			case('HTML'):
		  				$data['_content'] = '/chmanager/contents/contents_html';
		  			break;
		  			case('BOARD'):
		  				$data['_content'] = '/chmanager/contents/contents_board';
		  			break;
		  			case('GALLERY'):
		  				$data['_content'] = '/chmanager/contents/contents_gallery';
		  			break;
		  			case('PLUGIN'):
		  				$data['_content'] = '/chmanager/contents/contents_plugin';
		  				break;
		  			default:
		  				$data['_content'] = '/chmanager/contents/contents_html';
		  			break;
		  		}
	  		}else{
	  			//$data['_content'] = 'chmanager/contents/contents_html';
	  		}
	
	  	}else{
	  		$data['_content'] = 'chmanager/contents/contents_html';
	  	}
	  	
	  	return $data;
	}
	
	function update(){
		$id= $this->input->post('id');
		$content = $this->input->post('content');
		$data = array(
				'title' => $this->input->post('title') ,
				'keyword' => $this->input->post('keyword') ,
				'description' => $this->input->post('description') ,
				'plugin' => $this->input->post('plugin') ,
				'mod_date' => date("Y-m-d H:i",time()) ,
				'mod_uid' => 'admin'
		);
		$this->db->where('menu_id', $id);
		$this->db->update('ch_config_contents', $data);
		
		$this->db->select("*");
		$this->db->where('menu_id', $id);
		$query = $this->db->get('ch_config_contents');
		$page_type = null;
		if($query->num_rows() > 0){
			$row = $query->row_array();
			$page_type = $row['page_type'];
		}

		switch($page_type){
			case('HTML'):
				write_file($this->config->item('ch_path_html').$id.'.html', $content);
			break;
			case('BOARD'):
				$cfg = array(
						'board_skin' => $this->input->post('board_skin'),
						'limit_title' => $this->input->post('limit_title'),
						'list_cnt' => $this->input->post('list_cnt'),
						'secret_yn' =>  $this->input->post('secret_yn'),
						'auth_write' => $this->input->post('auth_write'),
						'auth_view' => $this->input->post('auth_view'),
						'auth_reply' => $this->input->post('auth_reply'),
						'auth_comment' => $this->input->post('auth_comment'),
				);
				$this->db->where('menu_id', $id);
				$this->db->update('ch_config_board', $cfg);
			break;
		
		}

	}
	
	function delete(){
		$id = $this->input->post('id');
		$this->db->where('menu_id', $id);
		$this->db->delete('ch_config_contents');
		
	}
	
}
?>